/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.communication.event.impl;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.gokeep.common.config.properties.CommunicationProperties;
import org.gokeep.common.constant.enums.ClientTypeEnum;
import org.gokeep.common.dto.TokenPayload;
import org.gokeep.common.dto.UserClientSessionAttr;
import org.gokeep.communication.event.Event;
import org.gokeep.communication.proto.CommandProto;
import org.gokeep.communication.proto.MessageProto;
import org.smartboot.socket.transport.AioSession;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ConnectionEvent extends Event {

    @SneakyThrows
    @Override
    public void process(AioSession session, MessageProto.Message message) {
        TokenPayload tokenPayload = getTokenPayload();
        String userId = tokenPayload.getUserId();
        ClientTypeEnum clientType = tokenPayload.getClientType();
        // 第一次连接之后，需要缓存session信息以及和用户的映射关系到本地缓存
        sessionCacheComponent.putShareWithClientSession(clientType, userId, session.getSessionID());
        sessionCacheComponent.putLocalUserAioSession(session, userId, clientType);
        log.info("Client[{}] -> with server connection", session.getRemoteAddress());
    }

    @Override
    protected CommandProto.CommandEnum getEventType() {
        return CommandProto.CommandEnum.CONNECTION;
    }
}
